package com.movies.dao;

import com.movies.pojo.Cinema;
import com.movies.pojo.Program;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MovieDaoImpl extends BaseDao implements MovieDao {

    @Override
    /**创建一个查询所有电影院信息方法*/
    public List queryAllCinema() {
        return null;
    }

    @Override
    /**创建一个查询电影院主页信息方法
     *通过页面携带的影院id跳转到该主页
     * 通过影院id查询影院信息
     * */
    public List queryCinema(int cinemaid) {
        String sql = "SELECT brandname, address,phone,`server` " +
                "FROM cinema c JOIN brand b ON c.`brandid`=b.`brandid` WHERE cinema = ?;";
        Object[] obj = {cinemaid};
        ResultSet rs = query(sql,obj);
        Cinema cinema = null;
        List list = new ArrayList();
        try{
            while(rs.next()){
                cinema = new Cinema();
                cinema.setBrandName(rs.getString("brandname"));
                cinema.setAddress(rs.getString("address"));
                cinema.setPhone(rs.getString("phone"));
                cinema.setServer(rs.getString("server"));
                list.add(cinema);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            close();
        }
        return list;
    }

    @Override
    /**创建一个查询播放档期计划方法
     * 通过页面提交的电影和影院id
     * 查询对应的档期
     * */
    public List queryProgram(int cinemaid,int movieid) {
        String sql = "SELECT begintime,endtime,l.languagename,h.hallname,price " +
                "FROM program p JOIN `language` l ON l.`languageid` = p.`languageid` " +
                "JOIN hall h ON h.`hallid`=p.`hallid` WHERE cinemaid=? AND movieid = ? AND  playdate=?";
        Object[] obj = {cinemaid,movieid};
        ResultSet rs = query(sql,obj);
        Program program = null;
        List list = new ArrayList();
        try{
            while (rs.next()){
                program = new Program();
                program.setBeginTime(rs.getString("begintime"));
                program.setEndTime(rs.getString("endtime"));
                program.setLanguage(rs.getString("languagename"));
                program.setHallName(rs.getString("hallname"));
                program.setPrice(rs.getDouble("price"));
                list.add(program);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            close();
        }
        return list;
    }
}
